﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Antlr.Runtime;

namespace antlr_truyencuoihetno
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label1_Click(object sender, EventArgs e)
        {
            sentence_textbox.Text = ((Label)sender).Text;
            button1_Click(null, null);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string inputString = sentence_textbox.Text.ToLower();

            var input = new ANTLRStringStream(inputString);
            var lexer = new hetnoLexer(input);
            var tokens = new CommonTokenStream(lexer);
            var parser = new hetnoParser(tokens);
            try
            {
                var result = parser.prog();
                var tree = result.Tree;
                //textBox2.Text = tree.ToStringTree();
                //System.Windows.Forms.MessageBox.Show(tree.ToStringTree());

                var treeNodeDrawable = new ASTTreeNode(tree);
                if (pictureBox1.Image != null)
                {
                    pictureBox1.Image.Dispose();
                    pictureBox1.Image = null;
                }
                Image image = new VisualAST(treeNodeDrawable).Draw();
                pictureBox1.Image = image;
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Hiện tại chưa phân tích được mẫu câu này.");
            }
        }

    }
}
